//
//  用户信息设置
//
//  Created by 周述坚 on 16/11/14.
//  Copyright © 2016年 周述坚. All rights reserved.
//

#import "JPDoctorRegisteVC.h"
#import "JPDoctorHeaderView.h"
#import "JPTimeTypeLayout.h"
#import "JPChatViewController.h"
#import "JPDoctorTimeCell.h"
#import "HMAdditions.h"
#import "InterfaceTool.h"

#define widthW self.view.bounds.size.width
#define heightH self.view.bounds.size.height


@interface JPDoctorRegisteVC ()<UIScrollViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong)UIImageView *customView;

@end
NSString *rig = @"timeCell";

@implementation JPDoctorRegisteVC {
    BOOL _isSelect;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    [self setupUI];
    _isSelect = NO;
    self.navigationItem.title = @"医生详情";
}

/**
 *  初始化主界面
 */
- (void)setupUI {
    // 创建头视图-- 加载xib
    JPDoctorHeaderView *headerView = [[[NSBundle mainBundle] loadNibNamed:@"JPDoctorHeaderView" owner:nil options:nil] lastObject];
    [self.view addSubview:headerView];
    headerView.doctorDetail = self.doctorDetail;
    // 设置约束
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.mas_topLayoutGuide).offset(0);
        make.right.left.equalTo(self.view).offset(0);
        make.height.mas_equalTo(200);
    }];

    // 创建滚动视图
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    [self.view addSubview:scrollView];
    scrollView.backgroundColor = [UIColor whiteColor];
    scrollView.contentSize = CGSizeMake(widthW *3, 0);
    scrollView.pagingEnabled = YES;
    scrollView.bounces = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    [scrollView endEditing:NO];
    // 设置代理
    scrollView.delegate = self;
    // 设置约束
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headerView.mas_bottom).offset(5);
        make.left.right.bottom.equalTo(self.view).offset(0);
    }];
    
    // 让约束立即生效
    [scrollView setNeedsLayout];
    // 如果需要的时候就布局
    [scrollView layoutIfNeeded];

    for (int i = 0; i < 2; i ++) {
        UITextView *textView = [[UITextView alloc] init];
        [scrollView addSubview:textView];
        
        if (i == 0) {
            [[InterfaceTool sharedInterfaceTool] getDoctorConditionSuccess:^(id response) {
                NSDictionary *result = response[@"data"];
                NSString *receivedConditionString = result[@"receiving_setting_extra"];
                
                for (NSString *str in result[@"receiving_settings"]) {
                    receivedConditionString = [receivedConditionString stringByAppendingFormat:@"\n\n%@", str];
                }
                textView.text = receivedConditionString;
                textView.textAlignment = NSTextAlignmentCenter;
            } failure:^(NSError *error) {
                textView.text = @"";
                NSLog(@"%@", error);
            }];
        } else {
            [[InterfaceTool sharedInterfaceTool] getDoctorProfileSuccess:^(id response) {
                NSDictionary *result = response[@"data"];
                textView.text = result[@"introduction"];
                NSLog(@"%@", result[@"introduction"]);
            } failure:^(NSError *error) {
                textView.text = @"";
                NSLog(@"%@", error);
            }];
        }
        
        textView.font = [UIFont systemFontOfSize:20];
        [textView setEditable:NO];

        CGFloat lableX = widthW * i;
        
        textView.frame = CGRectMake(lableX, 0, widthW, scrollView.bounds.size.height);
        
    }
    // 定义一个预约时间的视图的view
    UIView *time = [[UIView alloc] init];
    time.frame = CGRectMake(widthW * 2, 0, widthW, heightH - 264);
    [scrollView addSubview:time];
    
    // 添加滚动视图
    JPTimeTypeLayout *layout = [JPTimeTypeLayout new];
    
    
    UICollectionView *timeView = [[UICollectionView alloc] initWithFrame: CGRectMake(0, 20, widthW, widthW * 0.5 ) collectionViewLayout:layout];
    timeView.backgroundColor = [UIColor lightGrayColor];
    [time addSubview:timeView];
    timeView.dataSource = self;
    timeView.delegate = self;
    [timeView registerClass:[JPDoctorTimeCell class] forCellWithReuseIdentifier:rig];
    
    // 设置底部按钮
    UIButton *botton = [[UIButton alloc] init];
     [time addSubview:botton];
    [botton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(time).offset(10);
        make.right.equalTo(time).offset(-10);
        make.height.mas_offset(36);
        make.bottom.mas_offset(-20);
    }];

    botton.backgroundColor = [UIColor orangeColor];
    [botton setTitle:@"点击咨询" forState:UIControlStateNormal];
   
    //给按钮添加点击事件
    [botton addTarget:self action:@selector(chatAction) forControlEvents:UIControlEventTouchUpInside];
    
    // 自定义滚动视图
    _customView = [[UIImageView alloc] init];
    //self.customView = customView;
    [self.view addSubview:_customView];

    _customView.frame = CGRectMake(0, 265, widthW * 0.3333, 2);
    _customView.image = [UIImage imageNamed:@"link_button_01"];
    
    // 滚动代码块
    void(^complection01)() = ^(){
        [UIView animateWithDuration:0.5 animations:^{
            _customView.frame = CGRectMake(0, 265, widthW * 0.3333, 2);
            [scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
        }];
    };
    headerView.completion01 = complection01;
    
    void(^complection02)() = ^(){
        [UIView animateWithDuration:0.5 animations:^{
            _customView.frame = CGRectMake(widthW * 0.3333, 265, widthW * 0.3333, 2);
            [scrollView setContentOffset:CGPointMake(widthW, 0) animated:YES];
        }];
    };
    
    headerView.completion02 = complection02;
    
    void(^complection03)() = ^(){
        [UIView animateWithDuration:0.5 animations:^{
            _customView.frame = CGRectMake(widthW * 0.6666, 265, widthW * 0.6666, 2);
            [scrollView setContentOffset:CGPointMake(widthW * 2, 0) animated:YES];
        }];
    };
    headerView.completion03 = complection03;
}

#pragma mark scrollView的代理方法

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    //scrollView
    
   CGFloat contentX = scrollView.contentOffset.x;

    CGFloat minus =  contentX - widthW;
    
    CGFloat value = minus / widthW;

    if (value == 0.0) {
        [UIView animateWithDuration:0.5 animations:^{
            _customView.frame = CGRectMake(widthW * 0.3333, 265, widthW * 0.3333, 2);
        }];
    } else if (value == -1){
        [UIView animateWithDuration:0.5 animations:^{
            _customView.frame = CGRectMake( 0, 265, widthW * 0.3333, 2);
        }];
    
    } else if (value == 1){
        [UIView animateWithDuration:0.5 animations:^{
            _customView.frame = CGRectMake( widthW * 0.6666, 265, widthW * 0.3333, 2);
        }];
    
    }
}

#pragma make collectionView的代理方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 32;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    JPDoctorTimeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:rig forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    
    switch (indexPath.row) {
        case 1:
            cell.textLabel.text = @"周一";
            break;
        case 2:
            cell.textLabel.text = @"周二";
            break;
        case 3:
            cell.textLabel.text = @"周三";
            break;
        case 4:
            cell.textLabel.text = @"周四";
            break;
        case 5:
            cell.textLabel.text = @"周五";
            break;
        case 6:
            cell.textLabel.text = @"周六";
            break;
        case 7:
            cell.textLabel.text = @"周日";
            break;
        case 8:
            cell.textLabel.text = @"上午";
            break;
        case 9:
            cell.textLabel.text = @"有号";
            break;
        case 16:
            cell.textLabel.text = @"下午";
            break;
        case 20:
            cell.textLabel.text = @"有号";
            break;
        case 24:
            cell.textLabel.text = @"晚上";
            break;
        case 29:
            cell.textLabel.text = @"有号";
            break;
            
        default:
            break;
    }
    
    return cell;

}

/**
 *  点击咨询按钮
 */
- (void)chatAction {
    JPChatViewController *chatVC = [JPChatViewController new];
    [self.navigationController pushViewController:chatVC animated:YES];
}

#pragma mark delegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    JPDoctorTimeCell *cell = (JPDoctorTimeCell *)[collectionView cellForItemAtIndexPath:indexPath];
    if ((indexPath.row == 9 || indexPath.row == 20 || indexPath.row == 29 ) && _isSelect == NO) {
        cell.backgroundColor = [UIColor lightGrayColor];
        cell.textLabel.text = @"已预约";
        cell.textLabel.textColor = [UIColor whiteColor];
        _isSelect = YES;
    }else if((indexPath.row == 9 || indexPath.row == 20 || indexPath.row == 29 ) && _isSelect == YES){
        cell.textLabel.text = @"有号";
        cell.backgroundColor = [UIColor whiteColor];
        cell.textLabel.textColor = [UIColor hm_colorWithRed:22 green:168 blue:150];
        _isSelect = NO;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
